import router from "./router";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import config from "./config";
import store from "./store";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = ["/login"];

let username = localStorage.getItem("username") || "";
let password = localStorage.getItem("password") || "";
if (username && password) {
  store.state.username = username;
  store.state.password = password;
}

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  // document.title = getPageTitle(to.meta.title);
  document.title = config.title;

  const isLogin = store.getters.getUser;
  if (isLogin) {
    next();
  } else {
    // console.log(whiteList.indexOf(to.path), 'whiteList.indexOf(to.path)')
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
    }
  }
});

// finish progress bar
router.afterEach(() => {
  NProgress.done();
});
